﻿package {
	import flash.display.*;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.*;
	
	import mx.controls.*;
	import mx.controls.listClasses.*;
	import mx.events.ResizeEvent;
	
	public class CheckboxPreviewRenderer extends ListItemRenderer {
		private var preview:Sprite;
		private var checkbox:CheckBox;
		public function CheckboxPreviewRenderer() {
			super();
			init();
			this.addEventListener(ResizeEvent.RESIZE, resizeHd);
		}
		private function init():void {
			checkbox = new CheckBox();
			checkbox.addEventListener(MouseEvent.CLICK, checkHd);
			addChild(checkbox);
			preview = new Sprite();
			preview.alpha = 0.5;
			preview.buttonMode = true;
			preview.addChild(new IconPreview());
			preview.addEventListener(MouseEvent.MOUSE_OVER, overHd);
			preview.addEventListener(MouseEvent.MOUSE_OUT, outHd);
			preview.addEventListener(MouseEvent.CLICK, previewHd);
			addChild(preview);
		}
		private function overHd(e:Event):void {
			e.target.alpha = 1;
		}
		private function outHd(e:Event):void {
			e.target.alpha = 0.5;
		}
		private function resizeHd(e:Event):void {
    		layout()
    	}
    	private function layout():void {
    		checkbox.x = 2;
    		checkbox.y = 9;
    		this.setChildIndex(checkbox,this.numChildren - 1); 
    		preview.x = this.width - preview.width - 2;
    		preview.y = (this.height - preview.height)*0.5;
    		this.setChildIndex(preview,this.numChildren - 1); 
    		
    	}
    	override public function set data(d:Object):void {
      		super.data = d;
      		if(d.selected){
      			checkbox.selected = true;
      		}else{
      			checkbox.selected = false;
      		}
    	}
    	private function checkHd(e:Event):void {
    		var list:List = listData.owner as List;
    		//先设置状态
			if(checkbox.selected){
				list.selectedItem.selected = true;
			}else{
				list.selectedItem.selected = false;
			}
			//发送事件
			var checkEvent:Event = new Event("itemcheck");
    		list.dispatchEvent(checkEvent);
    	}
    	private function previewHd(e:Event):void {
    		var list:List = listData.owner as List;
    		var previewEvent:Event = new Event("itempreview");
    		list.dispatchEvent(previewEvent);
    	}

	}
}